//
//  HBMineCtrl.m
//  HuoBao
//
//  Created by sky on 15/12/4.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBMineCtrl.h"
#import "HBSettingCtrl.h"
#import "HBIncomeDetailCtrl.h"
#import "HBMyRedPacketListCtrl.h"
#import "HBMyMsgListCtrl.h"
#import "InvitationViewCtrl.h"
#import "HBMyBankAccountCtrl.h"
#import "HBGetUserStatusManager.h"
#import "HBMySecurityCtrl.h"
#import "HBMyProblemsCtrl.h"
#import "HBLoginManager.h"
#import "UIImageView+WebCache.h"


#define WIDTH (SCREEN_WIDTH - 1)/3
#define HEIGHT 100

#define STARTY 386/2 + 12
#define SPACE 0.5

@interface HBMineCtrl ()
{
    UIScrollView *_bgview;
    UILabel *_name;
    UIImageView *_headvi;
    NSArray *_arr;
}

@end

@implementation HBMineCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
}


-(void)buildLayout
{
    _bgview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _bgview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_bgview];
    
    _arr = [[NSArray alloc]initWithObjects:@"邀请码",@"红包",@"我的银行卡",@"收益明细",@"我的消息",@"安全保障",@"常见问题" ,nil];
    
    UIImage *titleim = [UIImage imageNamed:@"mine_titleim"];
    UIImageView *titlevi = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, titleim.size.height)];
    titlevi.image = titleim;
    titlevi.userInteractionEnabled = YES;
    [_bgview addSubview:titlevi];
    
    
    UIImage *headim = [UIImage imageNamed:@"mine_holdplace"];
    UIView *bleow = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 -(headim.size.width/2 + 2), 122/2, headim.size.width + 4, headim.size.height + 4)];
    bleow.backgroundColor = [LCCommon getColor:@"c9c9c9"];
    bleow.layer.cornerRadius = bleow.width/2; 
    [titlevi addSubview:bleow];
    
    
    _headvi = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, headim.size.width, headim.size.height)];
    _headvi.layer.cornerRadius = _headvi.width/2;
    _headvi.clipsToBounds = YES;
    _headvi.image = headim;
    [_headvi setImageWithURL:[NSURL URLWithString:[HBLoginManager sharedHBLoginManager].currentUserInfo.headImg] placeholderImage:headim];
    [bleow addSubview:_headvi];
    
    NSString *str = [[NSString alloc]init];
    if ([HBLoginManager sharedHBLoginManager].currentUserInfo.username.length == 0) {
        str = [HBLoginManager sharedHBLoginManager].currentUserInfo.mobile;
    }else{
        str = [NSString stringWithFormat:@"%@  %@",[HBLoginManager sharedHBLoginManager].currentUserInfo.username,[HBLoginManager sharedHBLoginManager].currentUserInfo.mobile];
    }
    _name = [self LabelWithRect:CGRectMake(0, 122/2 + bleow.height + 12 , SCREEN_WIDTH, 16) textColor:[LCCommon getColor:@"ffffff"] textstr:str textfont:16 textstyle:NSTextAlignmentCenter];
    [titlevi addSubview:_name];
    
    
    UIImage *setim = [UIImage imageNamed:@"mine_set"];
    UIImageView *setvi = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 42/2 - setim.size.width, 78/2, setim.size.width, setim.size.height)];
    setvi.image = setim;
    [titlevi addSubview:setvi];

    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(setvi.origin.x - 15, setvi.origin.y - 15, setvi.width + 30, setvi.size.height + 30)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(topTosetting) forControlEvents:UIControlEventTouchUpInside];
    [titlevi addSubview:btn];
    
    [self MakeBleowView];
    
    _bgview.contentSize = CGSizeMake(SCREEN_WIDTH, STARTY + 3 * 100 + 30 + TABBAR_HEIGHT);
}


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    [_headvi setImageWithURL:[NSURL URLWithString:[HBLoginManager sharedHBLoginManager].currentUserInfo.headImg] placeholderImage:[UIImage imageNamed:@"mine_holdplace"]];
    
    NSString *str = [[NSString alloc]init];
    if ([HBLoginManager sharedHBLoginManager].currentUserInfo.username.length == 0) {
        str = [HBLoginManager sharedHBLoginManager].currentUserInfo.mobile;
    }else{
        str = [NSString stringWithFormat:@"%@  %@",[HBLoginManager sharedHBLoginManager].currentUserInfo.username,[HBLoginManager sharedHBLoginManager].currentUserInfo.mobile];
    }
    _name.text = str;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeInformation) name:@"UpDataInformation"  object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}


#pragma mark - topTosetting

-(void)topTosetting{
    LLog(@"topTosetting");
    HBSettingCtrl *settingCtrl = [[HBSettingCtrl alloc]init];
    [self.navigationController pushViewController:settingCtrl animated:YES];
}

#pragma mark - MakeBleowView

-(void)MakeBleowView
{
    UIImage *zhanwei = [UIImage imageNamed:@"我的银行卡"];
    for (int i = 0; i < 9; i ++) {
        int x = i%3;
        int y = i/3;
        UIButton *mineBtn = [[UIButton alloc]initWithFrame:CGRectMake(x * (WIDTH + SPACE) , y * (HEIGHT + SPACE) + STARTY, WIDTH , HEIGHT)];
        [mineBtn setBackgroundColor:[LCCommon getColor:@"37383d"] forState:UIControlStateNormal];
        [mineBtn setBackgroundColor:[LCCommon getColor:@"37383d"] forState:UIControlStateDisabled];
        [mineBtn setBackgroundColor:[LCCommon getColor:@"313339"] forState:UIControlStateHighlighted];
        [mineBtn addTarget:self action:@selector(mineBtnPress:) forControlEvents:UIControlEventTouchUpInside];
        mineBtn.tag = 1000 + i;
        if (i == 7 || i == 8) {
            mineBtn.enabled = NO;
        }
        [_bgview addSubview:mineBtn];
        UIImage *mineim = [[UIImage alloc]init];
        if (i < 7) {
            
            mineim = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_arr objectAtIndex:i]]];
            
        }
        
        UIImageView *mineVi = [[UIImageView alloc]initWithFrame:CGRectMake(mineBtn.origin.x + (WIDTH/2 - zhanwei.size.width/2 ), mineBtn.origin.y + (HEIGHT/2 - zhanwei.size.height/2), zhanwei.size.width, zhanwei.size.height)];
        mineVi.image = mineim;
        [_bgview addSubview:mineVi];
    }
}


-(void)mineBtnPress:(UIButton *)sender{
    UIViewController *ctrl = [[UIViewController alloc]init];

    switch (sender.tag) {
        case 1000:
            ctrl = [[InvitationViewCtrl alloc]init];
            break;
            
        case 1001:
            ctrl = [[HBMyRedPacketListCtrl alloc]init];
            break;
        case 1002:
        {
            ctrl = [[HBMyBankAccountCtrl alloc]init];
            
            /*
            if (![HBGetUserStatusManager sharedHBGetUserStatusManager].userStatusInfo)
            {
                [CustomLoadingView showHudToView:nil];
                [HBGetUserStatusManager requestToGetUserStatusWithResult:^(BOOL isSuccess, NSString *msg, NSError *error) {
                    [CustomLoadingView hideHudForView:nil];
                    if (isSuccess)
                    {
                        if ([HBGetUserStatusManager sharedHBGetUserStatusManager].isAllAuthor)
                        {
                            ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_YES;
                        }
                        else
                        {
                            if (![[[HBGetUserStatusManager sharedHBGetUserStatusManager].userStatusInfo bindCardStatus] integerValue])
                            {
                                ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_None;
                            }
                            else
                            {
                                ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_YES;
                            }
                            
                        }
                    }
                    else
                    {
                        DISSMISS_ERR(error?[error description]:msg);
                    }
                    
                    
                }];
            }
            else
            {
                if (![[[HBGetUserStatusManager sharedHBGetUserStatusManager].userStatusInfo bindCardStatus] integerValue])
                {
                    ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_None;
                }
                else
                {
                    ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_YES;
                }
            }
            */
//            [CustomLoadingView showHudToView:nil];
            [CustomLoadingView hideHudForView:self.view];
            [CustomLoadingView showHudToView:[UIApplication sharedApplication].keyWindow];
            [HBGetUserStatusManager requestToGetUserStatusWithResult:^(BOOL isSuccess, NSString *msg, NSError *error) {
//                [CustomLoadingView hideHudForView:nil];
                [CustomLoadingView hideHudForView:[UIApplication sharedApplication].keyWindow];
                if (isSuccess)
                {
                    if ([HBGetUserStatusManager sharedHBGetUserStatusManager].isAllAuthor)
                    {
                        ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_YES;
                    }
                    else
                    {
                        if (![[[HBGetUserStatusManager sharedHBGetUserStatusManager].userStatusInfo bindCardStatus] integerValue])
                        {
                            ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_None;
                        }
                        else
                        {
                            ((HBMyBankAccountCtrl *)ctrl).bindType = MyBankBindType_YES;
                        }
                        
                    }
                    [self.navigationController pushViewController:ctrl animated:YES];
                }
                else
                {
                    DISSMISS_ERR(error?[error description]:msg);
                }
                
                
            }];
            
            break;
        }
        case 1003:
            ctrl = [[HBIncomeDetailCtrl alloc]init];
            break;
        case 1004:
            ctrl = [[HBMyMsgListCtrl alloc]init];
            break;
        case 1005:
            ctrl = [[HBMySecurityCtrl alloc]init];
            break;
        case 1006:
            //HBMyProblemsCtrl
            ctrl = [[HBMyProblemsCtrl alloc]init];
            break;
            
        default:
            break;
    }
    if (sender.tag!=1002) {
        [self.navigationController pushViewController:ctrl animated:YES];
    }
    
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)changeInformation{
    [_headvi setImageWithURL:[NSURL URLWithString:[HBLoginManager sharedHBLoginManager].currentUserInfo.headImg] placeholderImage:[UIImage imageNamed:@"mine_holdplace"]];
    
    NSString *str = [[NSString alloc]init];
    if ([HBLoginManager sharedHBLoginManager].currentUserInfo.username.length == 0) {
        str = [HBLoginManager sharedHBLoginManager].currentUserInfo.mobile;
    }else{
        str = [NSString stringWithFormat:@"%@  %@",[HBLoginManager sharedHBLoginManager].currentUserInfo.username,[HBLoginManager sharedHBLoginManager].currentUserInfo.mobile];
    }
    _name.text = str;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
